package com.timoon.webcomponent.utility.helpinfo;

import java.util.List;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.timoon.sample.dao.model.InfoMedium;
import com.timoon.sample.utils.SystemConfig;

/**
 *<p>功能:帮助信息</p>
 *<p>accordion标题</p>
 *<p>Copyright:Copyright(c)2013</p>
 *<p>Company:sky</p>
 *@author 付乐天
 *@version 1.0 */

public class HelpListInfoPanel extends Panel {
	
	@SpringBean(name="sample.InfoMedium")
	private HelpInfoService service;
	
	private Class<? extends WebPage> targetPage;
	
	private PageParameters pageParameters;

	public HelpListInfoPanel(String id,PageParameters pageParameters,Class<? extends WebPage> targetPage) {
		super(id);
		// TODO Auto-generated constructor stub
		
		this.targetPage=targetPage;
		
		if(pageParameters.get("info_medium_id").isNull()){
			throw new RuntimeException("必须传入【info_medium_id】参数");
		}else{
			this.pageParameters=pageParameters;
		}
		
		renderPanel();
	}

	private void renderPanel() {
		// TODO Auto-generated method stub
		
		renderGuide();
		
		renderPaid();
		
		renderInfo();
		
		renderSend();
		
		renderService();
		
		renderContanct();
	}

	private void renderContanct() {
		// TODO Auto-generated method stub
		
	}

	private void renderService() {
		// TODO Auto-generated method stub
		this.renderListView(SystemConfig.HELP_INFO_PANEL_SERVICE_TYPE, "wk_help_service", "wk_help_service_link", "wk_help_service_label");
	}

	private void renderSend() {
		// TODO Auto-generated method stub
		this.renderListView(SystemConfig.HELP_INFO_PANEL_SEND_TYPE, "wk_help_send", "wk_help_send_link", "wk_help_send_label");
	}

	private void renderPaid() {
		// TODO Auto-generated method stub
		this.renderListView(SystemConfig.HELP_INFO_PANEL_PAID_TYPE, "wk_help_paid", "wk_help_paid_link", "wk_help_paid_label");
	}

	private void renderInfo() {
		// TODO Auto-generated method stub
		
		InfoMedium info=this.service.getInfoMediumById(Integer.valueOf(this.pageParameters.get("info_medium_id").toString()));
		
		add(new Label("wk_info_title",info.getName()));
		
		add(new Label("wk_info_content",info.getValue()));
		
	}

	private void renderGuide() {
		// TODO Auto-generated method stub
		
		this.renderListView(SystemConfig.HELP_INFO_PANEL_CELL1_TYPE, "wk_help_listview", "wk_help_link", "wk_help_label");
		
	}
	
	private void renderListView(Integer type,String listId,final String linkId,final String labelId){
		List<InfoMedium> infos=this.service.getInfoMediumByType(type);
		
		ListView<InfoMedium> listView=new ListView<InfoMedium>(listId,infos) {
			
			@Override
			protected void populateItem(ListItem<InfoMedium> arg0) {
				// TODO Auto-generated method stub
				
				InfoMedium info=arg0.getModelObject();
				
				PageParameters parameters=new PageParameters();
				parameters.set("info_medium_id", info.getId());
				
				BookmarkablePageLink link=new BookmarkablePageLink<>(linkId, targetPage,parameters);
				
				arg0.add(link);
				
				link.add(new Label(labelId,info.getName()));
				
				
			}
		};
		
		add(listView);
	}

}
